﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides methods to compute harmonic numbers. 
    /// </summary>
    [Serializable]
    public class HarmonicNumber
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HarmonicNumber"/> class.
        /// </summary>
        public HarmonicNumber()
        {
        }

        /// <summary>
        /// Computes the n-th harmonic number.
        /// </summary>
        /// <param name="index">The index of the harmonic number.</param>
        /// <returns>The specified n-th harmonic number.</returns>
        public double HarmonicNumberAt(int index)
        {
            double result = 0;

            for (int i = 1; i <= index; i++)
            {
                result += 1.0 / i;
            }

            return result;
        }

        /// <summary>
        /// Computes the n-th harmonic number.
        /// </summary>
        /// <param name="index">The index of the harmonic number.</param>
        /// <returns>The specified n-th harmonic number.</returns>
        public Fraction HarmonicNumberAtAsFraction(int index)
        {
            Fraction result = new Fraction(1, 1);

            for (int i = 2; i <= index; i++)
            {
                result += new Fraction(1, i);
            }

            result.Cancel();

            return result;
        }
    }
}